package com.jeecg.p3.commonluckymoney.service.impl;

import com.jeecg.p3.commonluckymoney.dao.WxActCommonluckymoneyAwardsDao;
import com.jeecg.p3.commonluckymoney.dao.WxActCommonluckymoneyRecordDao;
import com.jeecg.p3.commonluckymoney.entity.WxActCommonluckymoneyAwards;
import com.jeecg.p3.commonluckymoney.entity.WxActCommonluckymoneyRecord;
import com.jeecg.p3.commonluckymoney.service.WxActCommonluckymoneyRecordService;
import org.jeecgframework.p3.base.vo.WeixinDto;
import org.jeecgframework.p3.core.common.utils.RandomUtils;
import org.jeecgframework.p3.core.util.oConvertUtils;
import org.jeecgframework.p3.core.utils.common.PageList;
import org.jeecgframework.p3.core.utils.common.PageQuery;
import org.jeecgframework.p3.core.utils.common.PageQueryWrapper;
import org.jeecgframework.p3.core.utils.common.Pagenation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("wxActCommonluckymoneyRecordService")
public class WxActCommonluckymoneyRecordServiceImpl implements WxActCommonluckymoneyRecordService {
	@Resource
	private WxActCommonluckymoneyRecordDao wxActCommonluckymoneyRecordDao;
	@Resource
	private WxActCommonluckymoneyAwardsDao wxActCommonluckymoneyAwardsDao;

	@Override
	public void doAdd(WxActCommonluckymoneyRecord wxActCommonluckymoneyRecord) {
		wxActCommonluckymoneyRecordDao.insert(wxActCommonluckymoneyRecord);
	}

	@Override
	public void doEdit(WxActCommonluckymoneyRecord wxActCommonluckymoneyRecord) {
		wxActCommonluckymoneyRecordDao.update(wxActCommonluckymoneyRecord);
	}

	@Override
	public void doDelete(String id) {
		wxActCommonluckymoneyRecordDao.delete(id);
	}

	@Override
	public WxActCommonluckymoneyRecord queryById(String id) {
		WxActCommonluckymoneyRecord wxActCommonluckymoneyRecord  = wxActCommonluckymoneyRecordDao.get(id);
		return wxActCommonluckymoneyRecord;
	}

	@Override
	public PageList<WxActCommonluckymoneyRecord> queryPageList(
		PageQuery<WxActCommonluckymoneyRecord> pageQuery) {
		PageList<WxActCommonluckymoneyRecord> result = new PageList<WxActCommonluckymoneyRecord>();
		Integer itemCount = wxActCommonluckymoneyRecordDao.count(pageQuery);
		PageQueryWrapper<WxActCommonluckymoneyRecord> wrapper = new PageQueryWrapper<>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
		List<WxActCommonluckymoneyRecord> list = wxActCommonluckymoneyRecordDao.queryPageList(wrapper);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public WxActCommonluckymoneyRecord creatRecord(
			WxActCommonluckymoneyAwards award,
			WeixinDto weixinDto) {
		//获取中奖序号
		Integer maxAwardsSeq = wxActCommonluckymoneyRecordDao.getMaxAwardsSeq(award.getId());
		//判断是否还有剩余奖品
		Integer nextAwardsSeq = oConvertUtils.getInt(maxAwardsSeq)+1;
		if(nextAwardsSeq>award.getTotalNum()){
			return null;//没有剩余奖品，不能中奖
		}
		//保存中奖纪录
		WxActCommonluckymoneyRecord record = new WxActCommonluckymoneyRecord();
		record.setId(RandomUtils.generateID());
		record.setActId(award.getActId());
		record.setOpenid(weixinDto.getOpenid());
		record.setAwardsSeq(nextAwardsSeq);
		record.setAwardsId(award.getId());
		record.setRecieveStatus("0");//未领取
		record.setRecieveTime(new Date());
		record.setJwid(weixinDto.getJwid());
		//record.setPhone(getBindPhone(weixinDto.getOpenid()));
		wxActCommonluckymoneyRecordDao.insert(record);
		//更新奖品数量
		wxActCommonluckymoneyAwardsDao.updateRemainNum(award.getId());
		record.setAwardsName(award.getAwardsName());
		return record;
	}

	@Override
	public WxActCommonluckymoneyRecord queryRecordByActIdAndOpenid(String actId, String openid) {
		return wxActCommonluckymoneyRecordDao.queryRecordByActIdAndOpenid(actId, openid);
	}

	@Override
	public List<WxActCommonluckymoneyRecord> queryListByActId(String actId) {
		return wxActCommonluckymoneyRecordDao.queryListByActId(actId);
	}
}
